package com.ggh.jinjilive.ui.mine;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.ggh.jinjilive.BaseApplication;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ActivityMineEarningsBinding;
import com.ggh.jinjilive.mobile.CommonMobile;
import com.ggh.jinjilive.ui.BaseActivity;
import com.ggh.jinjilive.utils.ARouterUtils;
import com.gyf.immersionbar.ImmersionBar;

import java.util.HashMap;
import java.util.Map;


@Route(path = ARouterUtils.MineEarningsActivity)
public class MineEarningsActivity extends BaseActivity<ActivityMineEarningsBinding> {
    //VM
    private MineViewModel mineViewModel;
    @Override
    public void initCreate() {
        super.initCreate();
        ImmersionBar.with(this).keyboardEnable(true).init();
        BaseApplication.getInstance().getAppComponent().doInjectionMain(this);
        mineViewModel = new ViewModelProvider(this,viewModelFactory).get(MineViewModel.class);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_mine_earnings;
    }

    @Override
    public void initView() {
        super.initView();
        setWhiteActionBarStyle("我的收益",true,true);
        //获取状态栏高度
        binding.llSpacing.setPadding(0, BarUtils.getStatusBarHeight(),0,0);
    }

    @Override
    public void initData() {
        super.initData();
        //我的收益
        mineViewModel.infoEarnings();
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //我的收益
        mineViewModel.mEarningsCommonMobile.observe(this, new Observer<CommonMobile>() {
            @Override
            public void onChanged(CommonMobile commonMobile) {
                if(!TextUtils.isEmpty(commonMobile.getDiamond_wallet())){
                    //总钻石数
                    binding.tv1ProfitDiamond.setText(commonMobile.getDiamond_wallet());
                }else {
                    //总钻石数
                    binding.tv1ProfitDiamond.setText(commonMobile.getMy_diamond_wallet());
                }
                if(!TextUtils.isEmpty(commonMobile.getSilver_wallet())){
                    //可提现积分数
                    binding.tv2ProfitDiamond.setText(commonMobile.getSilver_wallet());
                }else {
                    //可提现积分数
                    binding.tv2ProfitDiamond.setText(commonMobile.getMy_silver_wallet());
                }
            }
        });
        //立即兑换
        binding.exchangeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String diamond = binding.etProfitDiamond.getText().toString();
                if(!TextUtils.isEmpty(diamond)){
                    Map<String, Object> params = new HashMap<>();
                    params.put("silver_number", diamond);
                    //积分兑换钻石
                    mineViewModel.infoYbdhzs(params);
                }else {
                    ToastUtils.make().show("请输入兑换积分数量");
                }
            }
        });
        //交易明细
        binding.tradingButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance().build(ARouterUtils.MineDealDetailActivity)
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        .navigation();
            }
        });
    }
}
